package com.zxl.yjb.im.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.util.Locale;
import java.util.Map;

import io.rong.imkit.RongContext;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;

/**
 * 作者：songs on 2018/5/18 16:49
 * 邮箱：xjs250@163.com
 * 简介：必须在im初始化并登陆后使用；
 */
public class RongChatUtils {

    public static void startPrivateChat(Context context, String targetUserId, String title, @Nullable Map<String, String> map) {
        if (map == null || map.size() == 0) {
            RongIM.getInstance().startConversation(context, Conversation.ConversationType.PRIVATE, targetUserId, title);
            return;
        }
        if (context != null && !TextUtils.isEmpty(targetUserId)) {
            if (RongContext.getInstance() == null) {
                throw new ExceptionInInitializerError("RongCloud SDK not init");
            } else {
                Uri.Builder uriBuilder = Uri.parse("rong://" + context.getApplicationInfo().packageName).buildUpon()
                        .appendPath("conversation")
                        .appendPath(Conversation.ConversationType.PRIVATE.getName().toLowerCase(Locale.US))
                        .appendQueryParameter("targetId", targetUserId)
                        .appendQueryParameter("title", title);
                for (String strKey : map.keySet()) {
                    uriBuilder.appendQueryParameter(strKey, map.get(strKey));
                }
                Uri uri = uriBuilder.build();
                context.startActivity(new Intent("android.intent.action.VIEW", uri));
            }
        } else {
            throw new IllegalArgumentException();
        }
    }

}
